package com.binguo.api.service;

import com.binguo.api.form.PayCardUpdateForm;
import com.binguo.common.exception.ServiceException;
import com.binguo.model.api.vo.UpdatePayCardVO;
import com.binguo.model.mapper.PayCardMapper;
import com.binguo.model.model.PayCard;
import com.binguo.model.model.Shop;
import com.binguo.model.repository.PayCardRepository;
import com.binguo.service.ShopService;
import io.swagger.models.auth.In;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Condition;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

import static com.binguo.common.exception.ErrorCode.SHOP_NOT_EXISTS;

/**
 * @author yuritian
 * @Title: PayCardService
 * @ProjectName binguoDevelop
 * @Description: TODO
 * @date 2019-12-1113:29
 */
@Service
public class PayCardService {
    private static Logger logger = LoggerFactory.getLogger(PayCardService.class);

    @Resource
    PayCardMapper payCardMapper;

    @Autowired
    PayCardRepository payCardRepository;

    @Autowired
    ShopService shopService;

    public UpdatePayCardVO getPayCardPageList(String phoneNum,
                                              Integer pageNum){
        UpdatePayCardVO result = new UpdatePayCardVO();
        Integer shopId;
        Shop shop = shopService.getShopByPhone(phoneNum);
        if (shop == null){
            throw new ServiceException(SHOP_NOT_EXISTS);
        }
        shopId = shop.getId();
        Condition condition = new Condition(PayCard.class);
        condition.or().andEqualTo("shopid",shopId)
                .andEqualTo("deleted",0);
        List<PayCard> payCardList = payCardRepository.findByCondition(condition);
        List<PayCard> intranetPayCardList = new ArrayList<>();

        result.setCount(payCardList.size()/20 + 1);

        Integer limit = pageNum * 20;

        payCardList = payCardMapper.getUpdatePaycardList(shopId,limit);
        for (PayCard payCard : payCardList){
            payCard.setAccountbalance(payCard.getAccountbalance()+payCard.getWxAccountbalance()+payCard.getBzAccountbalance()+payCard.getYktAccountbalance());
            payCard.setWxAccountbalance(0L);
            payCard.setYktAccountbalance(0L);
            payCard.setBzAccountbalance(0L);
            intranetPayCardList.add(payCard);
        }

        result.setPayCardList(payCardList);

        return result;
    }

    public PayCard getPayCard(Integer shopId,
                              Integer customerId){

        Condition condition = new Condition(PayCard.class);
        condition.or().andEqualTo("customerid",customerId)
                .andEqualTo("shopid",shopId);

        List<PayCard> payCardList = payCardMapper.selectByCondition(condition);

        if (null == payCardList || payCardList.size() == 0){
            return null;
        }else {
            return payCardList.get(0);
        }
    }
}
